package com.aaa.jx.controller;

import com.aaa.jx.entity.TSdsSub;
import com.aaa.jx.service.TSdsSubService;
import com.aaa.jx.service.impl.TSdsSubServiceImpl;

import com.util.LayData;
import com.util.LayPage;
import com.util.PrintJsonData;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

/**
 * (TSdsSub)表控制层
 *
 * @author makejava
 * @since 2023-05-04 09:54:30
 */


@WebServlet(name = "TSdsSubServlet", value = "/TSdsSubServlet")
public class TSdsSubServlet extends HttpServlet {

    private TSdsSubService tSdsSubService = new TSdsSubServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Map<String, String[]> map = req.getParameterMap();
        TSdsSub tSdsSub=new TSdsSub();
        try {
            BeanUtils.populate(tSdsSub,map);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        }
        String sdssub = req.getParameter("sdssub");
        // 
        String method = req.getParameter("method");
        if (method==null){
            List<TSdsSub> tSdsSubs = tSdsSubService.queryBySub(sdssub);
            LayData success=new LayData(0,"",0L,tSdsSubs);
            PrintJsonData.printData(resp,success);

        }else if(method.equals("query")){
            String page = req.getParameter("page");
            String limit = req.getParameter("limit");
            LayPage layPage=new LayPage(page,limit);
            layPage.setStart(layPage.getStart());
            List<TSdsSub> tSdsSubs = tSdsSubService.queryAll(layPage);
            long count = tSdsSubService.count(null);

            LayData success=new LayData(0,"",count,tSdsSubs);
            PrintJsonData.printData(resp,success);
            return;
        }else if(method.equals("add")){
            tSdsSubService.insert(tSdsSub);
            LayData success=new LayData( 0L,null);
            PrintJsonData.printData(resp,success);
//            add(req,resp);
            return;
        }else if(method.equals("upd")){
            tSdsSubService.update(tSdsSub);
            LayData success=new LayData( 0L,null);
            PrintJsonData.printData(resp,success);
//            upd(req,resp);
            return;
        }else if(method.equals("del")){
            boolean b = tSdsSubService.deleteById(tSdsSub.getId());
            LayData success=new LayData();
            if (!b){
                success.setCode(1);
            }
            PrintJsonData.printData(resp,success);
//            del(req,resp);
            return;
        }else if(method.equals("check")){
            String sdsno = req.getParameter("sdsno");
            int i = Integer.parseInt(sdsno);
            TSdsSub tSdsSub1 = tSdsSubService.queryByNo(i);
            LayData success=new LayData();
            if(tSdsSub1!=null){
                success.setCode(1);
            }else {
                success.setCode(0);
            }
            PrintJsonData.printData(resp,success);
//            upd(req,resp);
            return;
        }

    }

}




